#ifndef _ICMPPACKETIMPL_H_
#define _ICMPPACKETIMPL_H_

#include "types.hpp"
#include "jhdefs.hpp"
#include "net/SocketDefs.hpp"
#include "stdlib/bugcheck.hpp"
using namespace JHCPP::stdlib;
#include <string>

NET_NAMESPACE_BEGIN

class CICMPPacketImpl
{
public:
	CICMPPacketImpl(int dataSize = 48);
	virtual ~CICMPPacketImpl();

	const UInt8* packet(bool init = true);
	virtual int packetSize() const = 0;

	virtual int maxPacketSize() const;
	UInt16 sequence() const;
	void setDataSize(int dataSize);
	int getDataSize() const;

	virtual struct timeval time(UInt8* buffer = 0, int length = 0) const = 0;

	virtual bool validReplyID(unsigned char* buffer, int length) const = 0;
	virtual std::string errorDescription(UInt8* buffer, int length) = 0;
	virtual std::string typeDescription(int typeId) = 0;

	static const UInt16 MAX_PACKET_SIZE;
	static const UInt16 MAX_SEQ_VALUE;

protected:
	UInt16 nextSequence();
	void resetSequence();
	virtual void initPacket() = 0;
	UInt16 checksum(UInt16 *addr, Int32 len);
private:
	UInt16	m_seq;
	UInt8*	m_pPacket;
	int		m_dataSize;

};


class CICMPv4PacketImpl : public CICMPPacketImpl
{
public:
	// ICMPv4 header
	struct Header 
	{
		UInt8 type;          // ICMP packet type
		UInt8 code;          // Type sub code
		UInt16 checksum;
		UInt16 id;
		UInt16 seq;
	};

	// compile-time shield against misalignment
#ifndef JH_ANDROID	
	jh_static_assert (offsetof(Header, code) == 0x01);
	jh_static_assert (offsetof(Header, checksum) == 0x02);
	jh_static_assert (offsetof(Header, id) == 0x04);
	jh_static_assert (offsetof(Header, seq) == 0x06);
#endif

	enum MessageType
	{
		ECHO_REPLY,
		ICMP_1,
		ICMP_2,
		DESTINATION_UNREACHABLE,
		SOURCE_QUENCH,
		REDIRECT,
		ICMP_6,
		ICMP_7,
		ECHO_REQUEST,
		ICMP_9,
		ICMP_10,
		TIME_EXCEEDED,
		PARAMETER_PROBLEM,
		TIMESTAMP_REQUEST,
		TIMESTAMP_REPLY,
		INFORMATION_REQUEST,
		INFORMATION_REPLY,
		MESSAGE_TYPE_UNKNOWN,  // non-standard default, must remain last but one
		MESSAGE_TYPE_LENGTH    // length indicator, must remain last
	};

	enum DestinationUnreachableCode
	{
		NET_UNREACHABLE,
		HOST_UNREACHABLE,
		PROTOCOL_UNREACHABLE,
		PORT_UNREACHABLE,
		FRAGMENTATION_NEEDED_AND_DF_SET,
		SOURCE_ROUTE_FAILED,
		DESTINATION_UNREACHABLE_UNKNOWN, // non-standard default, must remain last but one
		DESTINATION_UNREACHABLE_LENGTH   // length indicator, must remain last
	};

	enum RedirectMessageCode
	{
		REDIRECT_NETWORK,
		REDIRECT_HOST,
		REDIRECT_SERVICE_NETWORK,
		REDIRECT_SERVICE_HOST,
		REDIRECT_MESSAGE_UNKNOWN, // non-standard default, must remain last but one
		REDIRECT_MESSAGE_LENGTH   // length indicator, must remain last
	};

	enum TimeExceededCode
	{
		TIME_TO_LIVE,
		FRAGMENT_REASSEMBLY,
		TIME_EXCEEDED_UNKNOWN, // non-standard default, must remain last but one
		TIME_EXCEEDED_LENGTH   // length indicator, must remain last
	};

	enum ParameterProblemCode
	{
		POINTER_INDICATES_THE_ERROR,
		PARAMETER_PROBLEM_UNKNOWN, // non-standard default, must remain last but one
		PARAMETER_PROBLEM_LENGTH   // length indicator, must remain last
	};

	CICMPv4PacketImpl(int dataSize = 48);
	~CICMPv4PacketImpl();

	int packetSize() const;//header + data

	//IP header, ICMP header and data
	struct timeval time(UInt8* buffer = 0, int length = 0) const;
	bool validReplyID(UInt8* buffer, int length) const;
	virtual std::string errorDescription(UInt8* buffer, int length);
	virtual std::string typeDescription(int typeId);

	static const UInt16 MAX_PACKET_SIZE;
	static const std::string MESSAGE_TYPE[MESSAGE_TYPE_LENGTH];
	static const UInt8 DESTINATION_UNREACHABLE_TYPE; // 3
	static const UInt8 SOURCE_QUENCH_TYPE;           // 4
	static const UInt8 REDIRECT_MESSAGE_TYPE;        // 5
	static const UInt8 TIME_EXCEEDED_TYPE;           // 11
	static const UInt8 PARAMETER_PROBLEM_TYPE;       // 12

private:
	void initPacket();
	Header* header(UInt8* buffer, int length) const;
	UInt8* data(UInt8* buffer, int length) const;

	static const std::string DESTINATION_UNREACHABLE_CODE[DESTINATION_UNREACHABLE_LENGTH];
	static const std::string REDIRECT_MESSAGE_CODE[REDIRECT_MESSAGE_LENGTH];
	static const std::string TIME_EXCEEDED_CODE[TIME_EXCEEDED_LENGTH];
	static const std::string PARAMETER_PROBLEM_CODE[PARAMETER_PROBLEM_LENGTH];

	UInt16 m_seq;
};

NET_NAMESPACE_END

#endif
